package com.dtborad.myblog.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dtborad.myblog.entity.Posts;
import com.dtborad.myblog.entity.Records;
import com.dtborad.myblog.mapper.RecordsMapper;
import com.dtborad.myblog.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordsMapper, Records> implements RecordService {

    @Autowired
    private RecordsMapper recordsMapper;

    @Override
    public Page<Records> getAllRecordsByPageSize(int pagenum, int pagesize, Integer userid, String content) {
        LambdaQueryWrapper<Records> wrapper = new LambdaQueryWrapper<>();
        // 如果是管理员，展示全部数据，不需要添加任何过滤条件
        if (userid != null && userid == 1) {
            return recordsMapper.selectPage(new Page<>(pagenum, pagesize), null);
        }

        // 根据用户ID过滤
        if (userid != null) {
            wrapper.eq(Records::getUserId, userid);
        }

        // 根据内容过滤
        if (StrUtil.isNotBlank(content)) {
            wrapper.like(Records::getPrizeName, content);
        }

        // 分页查询
        return recordsMapper.selectPage(new Page<>(pagenum, pagesize), wrapper);
    }
}
